//
//  MainViewController.m
//  qunfa
//
//  Created by Admin on 2017/8/10.
//  Copyright © 2017年 cuixiaocun. All rights reserved.
//

#import "MainViewController.h"
#import "NavgationView.h"
#import "ScanCodeVIewController.h"
#import "HomePage.h"
#import "EditViewController.h"
#import "ManagementVC.h"
#import "LoginPage.h"
#import "RechargeCenter.h"
#import "CustomerServiceVC.h"
@interface MainViewController ()<CXCNavgationDelegate,EditDelegate>
{
    TPKeyboardAvoidingScrollView *bgScrollView;
    
}
@end

@implementation MainViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden =YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }


//    
    [self mainView];
}
- (void)returnBtnAction
{


}
- (void)mainView
{
    bgScrollView =[[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 0,CXCWidth, CXCHeight)];
    [bgScrollView setUserInteractionEnabled:YES];
    [bgScrollView setBackgroundColor:BGColor];
    [bgScrollView setShowsHorizontalScrollIndicator:YES];
    [self.view addSubview:bgScrollView];
    
    [bgScrollView setContentSize:CGSizeMake(CXCWidth, 1350*Width)];
    bgScrollView.showsVerticalScrollIndicator = FALSE;
    bgScrollView.showsHorizontalScrollIndicator = FALSE;
    UIImageView *btnView =[[UIImageView alloc]initWithFrame:CGRectMake(0,0 ,CXCWidth ,1350*Width )];
    btnView.image =[UIImage imageNamed:@"bg.png"];
    btnView.userInteractionEnabled = YES;
//    [btnView setBackgroundColor:NavColor];
    [bgScrollView addSubview:btnView];
    //替代导航栏的imageview
    UIImageView *topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CXCWidth, 72*Width)];
    topImageView.userInteractionEnabled = YES;
    topImageView.backgroundColor = [UIColor clearColor];
    [bgScrollView addSubview:topImageView];
    
   UIButton * _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.frame = CGRectMake(645*Width, 70*Width, 75*Width, 75*Width);
    _rightBtn.titleLabel.font =[UIFont boldSystemFontOfSize:15];
    [_rightBtn setImage:[UIImage imageNamed:@"icon_quit"] forState:UIControlStateNormal];
    [_rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [bgScrollView addSubview:_rightBtn];
    NSArray* bottomArr =@[@"信息编辑",@"信息管理",@"扫一扫",@"联系客服",@""];
    NSArray* topArr = @[@"1-bianji.png",@"2-guanli.png",@"3-saoyisao.png",@"4-kefu.png"];
    for (int i=0; i<4; i++) {
        //大按钮
        UIButton *btn =[[UIButton alloc]init];
        if (i==0){
            btn.frame =CGRectMake(250*Width,330*Width,450*Width,150*Width);
        }else if (i==1){
            btn.frame =CGRectMake(390*Width,579*Width,450*Width,150*Width);
        }else if (i==2) {
            btn.frame =CGRectMake(390*Width,810*Width,450*Width,150*Width);
        }else if (i==3){
            btn.frame =CGRectMake(240*Width,1030*Width,450*Width,150*Width);
        }
//        btn.backgroundColor =[UIColor redColor];
        [btn addTarget:self action:@selector(myBtnAciton:) forControlEvents:UIControlEventTouchUpInside];
        [btn.layer setCornerRadius:20*Width];
        btn.tag =i+300;
        btn.clipsToBounds=YES;
        btn.showsTouchWhenHighlighted = YES;

        //左边图片
        UIImageView *topImgV =[[UIImageView alloc]initWithFrame:CGRectMake(20*Width,15*Width,120*Width,120*Width)];
        topImgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",topArr[i]]];
        topImgV.tag =1100+i;
//        [topImgV setBackgroundColor:[UIColor whiteColor]];
//        [topImgV.layer setCornerRadius:2];
//        [topImgV.layer setBorderWidth:1];
//        [topImgV.layer setMasksToBounds:YES];
        topImgV.layer.borderColor =[UIColor colorWithRed:200/255.00 green:200/255.00 blue:200/255.00 alpha:0.3].CGColor;
        [btn addSubview:topImgV];
        //右边文字
        UILabel *botLabel =[[UILabel alloc]initWithFrame:CGRectMake(topImgV.right+ 20*Width,0*Width,250*Width,150*Width)];
        botLabel.textAlignment=NSTextAlignmentLeft;
        botLabel.font =[UIFont systemFontOfSize:18];
        botLabel.textColor =[UIColor whiteColor];
        botLabel.text =[NSString stringWithFormat:@"%@",bottomArr[i]];
        [btn addSubview:botLabel];

        
        [btnView addSubview:btn];
       
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)rightBtnAction
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"确定要退出吗？" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loginOut];

        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"点击取消");
        
    }]];
    
    
    
    [self presentViewController:alertController animated:YES completion:nil];
    


}
- (void)retunButton
{
    
}
- (void)myBtnAciton:(UIButton *)btn
{

    
    if (btn.tag==302) {
        if (![[PublicMethod getDataStringKey:@"IsLogin"] isEqualToString:@"HY"]) {
            [self login];
            return;
        }
        [self personCenter];
        
        
    }else if (btn.tag==300) {
        if (![[PublicMethod getDataStringKey:@"IsLogin"] isEqualToString:@"HY"]) {
            [self login];
            return;
        }
        EditViewController *homeVC =[[EditViewController alloc]init];
        homeVC.delegate =self;
        [self.navigationController pushViewController:homeVC animated:YES];
        NSLog(@"%@",@"asdtyui");
    }else if (btn.tag==301) {
        if (![[PublicMethod getDataStringKey:@"IsLogin"] isEqualToString:@"HY"]) {
            [self login];
            return;
        }
        ManagementVC *homeVC =[[ManagementVC  alloc]init];
        [self.navigationController pushViewController:homeVC animated:YES];
        NSLog(@"%@",@"asdtyui");
    }else if (btn.tag==303)
    {
        
        //         RechargeCenter*homeVC =[[RechargeCenter alloc]init];
        //        [self.navigationController pushViewController:homeVC animated:YES];
        //        NSLog(@"%@",@"asdtyui");
        CustomerServiceVC*homeVC =[[CustomerServiceVC alloc]init];
        [self.navigationController pushViewController:homeVC animated:YES];
        NSLog(@"%@",@"asdtyui");

        
    }else if(btn.tag==304)
    {
        
    }else if(btn.tag ==305)
    {

    
    }

}
- (void)personCenter
{
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    [dic1 setDictionary:@{
                          @"memberId":[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",[[PublicMethod getDataKey:member] objectForKey:@"id"]]] ,
                          }
     ];

    [PublicMethod AFNetworkPOSTurl:@"index.php/Home/Member/memberInfo" paraments:dic1  addView:self.view success:^(id responseDic) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseDic options:NSJSONReadingMutableContainers error:nil];
        if ([[NSString stringWithFormat:@"%@",[dict objectForKey:@"code"]]isEqualToString:@"0"]) {
           NSInteger timeOut= [self comparewithDate:[[[dict objectForKey:@"data"] objectForKey:@"member"] objectForKey:@"endtime"]];
            if (timeOut ==1)
            {
                [self getPushList];
            }else
            {
                [MBProgressHUD showSuccess:@"请联系客服" ToView:self.view];
                return ;
            }
            
        }
        
    } fail:^(NSError *error) {
        
    }];
}
- (void)getPushList
{
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    [dic1 setDictionary:@{
                          @"memberId":[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",[[PublicMethod getDataKey:member] objectForKey:@"id"]]] ,
                          }
     ];
    
    [PublicMethod AFNetworkPOSTurl:@"index.php/Home/Advert/pushAdvert" paraments:dic1  addView:self.view success:^(id responseDic) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseDic options:NSJSONReadingMutableContainers error:nil];
        if ([[NSString stringWithFormat:@"%@",[dict objectForKey:@"code"]]isEqualToString:@"0"])
        {
            NSArray*arr= [[NSArray alloc]init];
            arr=[dict objectForKey:@"data"];
            if(arr.count == 0)
            {
                [MBProgressHUD showSuccess:@"请添加广告" ToView:self.view];
            }
            HomePage *scanCodeVC =[[HomePage alloc]init];
            scanCodeVC.pushArr =[dict objectForKey:@"data"];
            [self.navigationController pushViewController:scanCodeVC animated:YES];
            NSLog(@"%@",@"asdtyui");
        }else
        {
//            [MBProgressHUD showSuccess:@"请添加广告" ToView:self.view];
        }
    } fail:^(NSError *error) {
        
    }];
}
-(void)needReloadDataEdit
{
    [MBProgressHUD showSuccess:@"保存成功" ToView:self.view];

}
- (void)login
{
    LoginPage *login =[[LoginPage alloc]init];
    [self.navigationController pushViewController:login animated:YES];
//    [self presentViewController:login animated:YES completion:^{
//        
//    }];


}
- (void)loginOut
{
    
    
    
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
   
    [dic1 setDictionary:@{
                          @"memberId":[NSString stringWithFormat:@"%@",[[PublicMethod getDataKey:member] objectForKey:@"id"]],
                          }];
    
    NSLog(@"%@",dic1);
    [PublicMethod AFNetworkPOSTurl:@"index.php/Home/Member/exitlogin" paraments:dic1  addView:self.view success:^(id responseDic) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseDic options:NSJSONReadingMutableContainers error:nil];
        if ([ [NSString stringWithFormat:@"%@",[dict objectForKey:@"code"]]isEqualToString:@"0"]) {
            //会员退出
            NSLog(@"%@",[dict objectForKey:@"data"]);
            [PublicMethod removeObjectForKey: member];
            [PublicMethod removeObjectForKey: @"IsLogin"];
            [MBProgressHUD showSuccess:[dict objectForKey:@"data"] ToView:self.view];
        }
        
    } fail:^(NSError *error) {
        
    }];
    
    
}

- (NSInteger)comparewithDate:(NSString*)bDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    NSLog(@"当前时间  =%@---到期%@",dateTime,bDate);
    
    NSInteger aa=0;
      NSComparisonResult result = [dateTime compare:bDate];
    if (result==NSOrderedSame)
    {
        //相等  aa=0
    }else if (result==NSOrderedAscending)
    {
        //bDate比aDate大
        aa=1;
    }else if (result==NSOrderedDescending)
    {
        //bDate比aDate小
        aa=-1;
        
    }
    
    return aa;
}
//  button1高亮状态下的背景色
- (void)button1BackGroundHighlighted:(UIButton *)sender
{
    
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
